package com.snow.useviewdemo.function.calendar;

import android.os.Bundle;

import com.snow.useviewdemo.R;
import com.snow.useviewdemo.base.BaseFragment;
import com.snow.useviewdemo.databinding.FragmentCalendarBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * author : zyt
 * e-mail : 632105276@qq.com
 * date   : 2019-12-25
 * desc   :
 */
public class CalendarFragment extends BaseFragment<FragmentCalendarBinding> {

    public static CalendarFragment getInstance(int year, int month) {
        CalendarFragment fragment = new CalendarFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("year", year);
        bundle.putInt("month", month);
//        bundle.putString("yearMonth", yearMonth);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_calendar;
    }

    @Override
    protected void setViewData() {
        if (getArguments() == null) {
            return;
        }
        int year = getArguments().getInt("year");
        int month = getArguments().getInt("month");
        String yearMonth = year + "-" + CalendarUtils.init().addZeroBefore(month);
//        List<CalendarData> calendarDataList = CalendarUtils.init().getCalendarDataList(yearMonth);
//        if (calendarDataList == null) {
//            CalendarUtils.init().putCalendarDataList(yearMonth, resetCalendarDataList(year, month, null));
//        }
//        mBinding.calendarView.setCalendarDataList(year, month, CalendarUtils.init().getCalendarDataList(yearMonth));
//需要延迟加载，否则不能加载出数据
//        new CountDownTimer(50, 50) {
//            @Override
//            public void onTick(long millisUntilFinished) {
//
//            }
//
//            @Override
//            public void onFinish() {
//                List<CalendarData> calendarDataList = CalendarUtils.init().getCalendarDataList(yearMonth);
//                if (calendarDataList == null) {
//                    CalendarUtils.init().putCalendarDataList(yearMonth, resetCalendarDataList(year, month, null));
//                }
//                mBinding.calendarView.setCalendarDataList(year, month, CalendarUtils.init().getCalendarDataList(yearMonth));
//            }
//        }.start();
        List<CalendarData> calendarDataList = CalendarUtils.init().getCalendarDataList(yearMonth);
        if (calendarDataList == null) {
            CalendarUtils.init().putCalendarDataList(yearMonth, resetCalendarDataList(year, month, null));
        }
        mBinding.calendarView.setCalendarDataList(year, month, CalendarUtils.init().getCalendarDataList(yearMonth));


    }

    /**
     * 重新设置日历数据
     *
     * @param year
     * @param month
     */
    public List<CalendarData> resetCalendarDataList(int year, int month, List<ClockInBean> clockInList) {
        List<CalendarData> calendarDataList = new ArrayList<>();
        if (clockInList == null) {
            clockInList = new ArrayList<>();
        }
        int monthLastDay = CalendarUtils.init().getMonthLastDay(year, month);
        int dayOfWeek = CalendarUtils.init().getFirstDayOfWeek(year, month);
        int dayOfMonth = CalendarUtils.init().getTodayOfMonth();
        //当前页面显示上月的数据，或者空
        for (int i = 0; i < dayOfWeek; i++) {
            CalendarData calendarData = new CalendarData();
            calendarData.setSolarNum("");
            calendarDataList.add(calendarData);
        }
        //设置当月数据
        for (int i = 0; i < monthLastDay; i++) {
            CalendarData calendarData = new CalendarData();
            calendarData.setYearMD(year + "-" + month + "-" + CalendarUtils.init().addZeroBefore(i + 1));
            calendarData.setSolarNum(String.valueOf(i + 1));
            calendarDataList.add(calendarData);
        }
//        calendarAdapter.setTodayPosition(dayOfMonth + dayOfWeek, CalendarUtils.init().getTodayMonth());
//        //计算共几行
//        int rowNum = (int) Math.ceil((monthLastDay + dayOfWeek) / 7f);
//        calendarAdapter.setItemWidthHeight(totalHeight / rowNum, rowNum, totalWidth / 7);

        CalendarUtils.init().addClockInInfoToCalendarInfo(year, month, dayOfWeek, calendarDataList, clockInList);

//        calendarAdapter.setDataList(calendarDataList);
        return calendarDataList;
    }
}
